package com.pzh.guli.service.edu.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.guli.service.edu.mapper.CourseDescriptionMapper;
import com.pzh.guli.service.edu.mapper.TeacherMapper;
import com.pzh.guli.service.edu.pojo.Course;
import com.pzh.guli.service.edu.mapper.CourseMapper;
import com.pzh.guli.service.edu.pojo.CourseDescription;
import com.pzh.guli.service.edu.pojo.Teacher;
import com.pzh.guli.service.edu.pojo.vo.*;
import com.pzh.guli.service.edu.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xzh
 * @since 2022-05-11
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CourseDescriptionMapper descriptionMapper;
    @Resource
    private TeacherMapper teacherMapper;
    /**
     * 保存课程
     * @param courseVo 课程实体
     * @return 课程id
     */
    @Override
    public String saveCourse(CourseVo courseVo) {
        // todo 这种多数据操作，增删改，需要加上事务

        Course course=new Course();
        BeanUtils.copyProperties(courseVo,course);
        course.setStatus(Course.COURSE_DRAFT);//未发布
        courseMapper.insert(course);

        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseVo.getDescription());
        courseDescription.setId(course.getId());
        descriptionMapper.insert(courseDescription);
        return course.getId();
    }

    @Override
    public CourseVo getCourseInfoById(String id) {
        Course course = courseMapper.selectById(id);
        if (course==null){
            return null;
        }
        CourseDescription courseDescription = descriptionMapper.selectById(id);
        CourseVo courseVo=new CourseVo();
        BeanUtils.copyProperties(course,courseVo);
        courseVo.setDescription(courseDescription.getDescription());
        return courseVo;
    }
    //更新课程
    @Override
    public String updateCourse(CourseVo courseVo) {
        Course course=new Course();
        BeanUtils.copyProperties(courseVo,course);
        course.setStatus(Course.COURSE_DRAFT);//未发布
        courseMapper.updateById(course);

        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseVo.getDescription());
        courseDescription.setId(course.getId());
        descriptionMapper.updateById(courseDescription);
        return course.getId();
    }

    @Override
    public Page<CourseDto> selectPage(Long page, Long limit, CourseQueryVo courseQueryVo) {
        QueryWrapper<CourseDto> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("c.gmt_create");
        String subjectId = courseQueryVo.getSubjectId();
        String parenId = courseQueryVo.getSubjectParenId();
        String teacherId = courseQueryVo.getTeacherId();
        String title = courseQueryVo.getTitle();
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("c.title",title);
        }
        if (!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("c.teacher_id",teacherId);
        }
        if (!StringUtils.isEmpty(parenId)){
            queryWrapper.eq("c.subject_parent_id",parenId);
        }
        if (!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("c.subject_id",subjectId);
        }
        Page<CourseDto> courseVoPage=new Page<>(page,limit);
        List<CourseDto> record= courseMapper.selectCoursePage(courseVoPage,queryWrapper);
        return courseVoPage.setRecords(record);
    }
    //发布课程 获取课程信息
    @Override
    public CoursePublishVo getPublishCourseById(String id) {
        CoursePublishVo vo= courseMapper.selectCoursePublishById(id);
        return vo;
    }
    //发布课程更改状态
    @Override
    public boolean updatePublish(String id) {
        Course course=new Course();
        course.setId(id);
        course.setStatus("Normal");//发布状态
        int i = courseMapper.updateById(course); //判断查询是否成功
        return i != 0;
    }
    //前台条件查询course
    @Override
    public List<Course> getCourseByQuery(WebCourseQuery webCourseQuery) {
        QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
        courseQueryWrapper.eq("status","Normal");//Normal表示发布状态
        if (!StringUtils.isEmpty(webCourseQuery.getSubjectId())){
            courseQueryWrapper.eq("subject_parent_id",webCourseQuery.getSubjectId());
        }
        if (!StringUtils.isEmpty(webCourseQuery.getSubjectParentId())){
            courseQueryWrapper.eq("subject_id",webCourseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(webCourseQuery.getBuyCount())){
            courseQueryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(webCourseQuery.getGmtModified())){
            courseQueryWrapper.orderByDesc("gmt_modified");
        }
        if(!StringUtils.isEmpty(webCourseQuery.getPrice())){
            courseQueryWrapper.orderByDesc("price");
        }
        return courseMapper.selectList(courseQueryWrapper);
    }
    //前端课程播放详细信息
    @Override
    public WebCourseVo getCoursePay(String id) {
       return courseMapper.getCoursePay(id);

    }
    //前台热门课程Top 8
    @Cacheable(value = "index",key = "'getTopCourse'")
    @Override
    public List<Course> getTopCourse() {
        QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("buy_count");
        courseQueryWrapper.eq("status","Normal");//只展示发布状态的课程
        courseQueryWrapper.last("limit 8");
        return courseMapper.selectList(courseQueryWrapper);
    }
    //订单模块查询course
    @Override
    public com.pzh.guli.service.base.dto.CourseDto geCourseDtoById(String courseId) {
        Course course = courseMapper.selectById(courseId);
        Teacher teacher = teacherMapper.selectById(course.getTeacherId());
        com.pzh.guli.service.base.dto.CourseDto courseDto=new com.pzh.guli.service.base.dto.CourseDto();
        courseDto.setId(course.getId());
        courseDto.setTitle(course.getTitle());
        courseDto.setCover(course.getCover());
        courseDto.setPrice(course.getPrice());
        courseDto.setTeacherName(teacher.getName());
        return courseDto;
    }
}
